package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.CodeSystem;
import com.innovations.inn.domain.ValueSetInfo;
import com.innovations.inn.domain.response.ResponseValueSetInfoCount;
import com.innovations.inn.service.ICodeSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典分类Controller
 *
 * @author dp
 * @date 2024-11-01
 */
@Api(tags = "字典分类")
@RestController
@RequestMapping("/inn/code/system")
public class CodeSystemController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    private ICodeSystemService codeSystemService;

    /**
     * 查询字典分类列表
     */
    @ApiOperation("查询字典分类列表")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/list")
    public TableDataInfo list(CodeSystem codeSystem) {
        startPage();
        List<CodeSystem> list = codeSystemService.selectCodeSystemList(codeSystem);
        return getDataTable(list);
    }

    /**
     * 导出字典分类列表
     */
    @PreAuthorize("@ss.hasPermi('inn:system:export')")
    @Log(title = "字典分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CodeSystem codeSystem) {
        List<CodeSystem> list = codeSystemService.selectCodeSystemList(codeSystem);
        ExcelUtil<CodeSystem> util = new ExcelUtil<CodeSystem>(CodeSystem.class);
        util.exportExcel(response, list, "字典分类数据");
    }

    /**
     * 获取字典分类详细信息
     */
    @ApiOperation("获取字典分类详细信息")
    @PreAuthorize("@ss.hasPermi('inn:system:query')")
    @GetMapping(value = "/{codeSystemId}")
    public AjaxResult getInfo(@PathVariable("codeSystemId") Long codeSystemId) {
        return success(codeSystemService.selectCodeSystemByCodeSystemId(codeSystemId));
    }

    /**
     * 新增字典分类
     */
    @ApiOperation("新增字典分类")
    @PreAuthorize("@ss.hasPermi('inn:system:add')")
    @Log(title = "字典分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody CodeSystem codeSystem) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        codeSystemService.completeData(codeSystem, userId, 1);
        return toAjax(codeSystemService.insertCodeSystem(codeSystem));
    }

    /**
     * 修改字典分类
     */
    @ApiOperation("修改字典分类")
    @PreAuthorize("@ss.hasPermi('inn:system:edit')")
    @Log(title = "字典分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody CodeSystem codeSystem) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        codeSystemService.completeData(codeSystem, userId, 2);
        return toAjax(codeSystemService.updateCodeSystem(codeSystem));
    }

    /**
     * 删除字典分类
     */
    @ApiOperation("删除字典分类")
    @PreAuthorize("@ss.hasPermi('inn:system:remove')")
    @Log(title = "字典分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codeSystemIds}")
    public AjaxResult remove(@PathVariable Long[] codeSystemIds) {
        return toAjax(codeSystemService.deleteCodeSystemByCodeSystemIds(codeSystemIds));
    }

    //region 字典明细

    /**
     * 查询自定义字典明细
     */
    @ApiOperation("查询自定义字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/custom/list")
    public TableDataInfo customlist(CodeSystem codeSystem) {
        startPage();
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询机构性质字典明细
     */
    @ApiOperation("查询机构性质字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/institution/property/list")
    public TableDataInfo institutionPpropertyList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("institution_property");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果类别字典明细
     */
    @ApiOperation("查询科研成果类别字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/class/list")
    public TableDataInfo researchOutcomesClassList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("research_outcomes_class");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询中医药成果类型字典明细
     */
    @ApiOperation("查询中医药成果类型字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/type/list")
    public TableDataInfo researchOutcomesTypeList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("research_outcomes_type");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果体现形式字典明细
     */
    @ApiOperation("查询科研成果体现形式字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/embodiment/list")
    public TableDataInfo researchOutcomesEmbodimentList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("research_outcomes_embodiment");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果阶段字典明细
     */
    @ApiOperation("查询科研成果阶段字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/stage/list")
    public TableDataInfo researchOutcomesStageList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("research_outcomes_stage");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果应用状态字典明细
     */
    @ApiOperation("查询科研成果应用状态字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/application/state/list")
    public TableDataInfo researchOutcomesApplicationStateList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("research_outcomes_application_state");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询商业需求类型字典明细
     */
    @ApiOperation("查询商业需求类型字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/business/demand/type/list")
    public TableDataInfo businessDemandTypeList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("business_demand_type");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询商业需求合作方式字典明细
     *
     * @param codeSystem
     * @return
     */
    @ApiOperation("查询商业需求合作方式字典明细")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/business/demand/ways/cooperation/list")
    public TableDataInfo businessWaysCooperationList(CodeSystem codeSystem) {
        startPage();
        codeSystem.setCodeSystemNo("business_demand_ways_cooperation");
        List<ValueSetInfo> list = codeSystemService.selectCodeSystemListValue(codeSystem);
        return getDataTable(list);
    }
    //endregion 字典明细

    //region 字典明细计数

    /**
     * 查询自定义字典明细计数
     */
    @ApiOperation("查询自定义字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @PostMapping("/custom/list/count")
    public TableDataInfo customlistCount(CodeSystem codeSystem) {
        if (codeSystem.getCodeSystemNo() == null) {
            throw new RuntimeException("字典编号不能为空");
        }
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询机构性质字典明细计数
     */
    @ApiOperation("查询机构性质字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/institution/property/list/count")
    public TableDataInfo institutionPpropertyListCount(CodeSystem codeSystem) {
        codeSystem.setCodeSystemNo("institution_property");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果类别字典明细计数
     */
    @ApiOperation("查询科研成果类别字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/class/list/count")
    public TableDataInfo researchOutcomesClassListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("research_outcomes_class");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询中医药成果类型字典明细计数
     */
    @ApiOperation("查询中医药成果类型字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/type/list/count")
    public TableDataInfo researchOutcomesTypeListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("research_outcomes_type");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果体现形式字典明细计数
     */
    @ApiOperation("查询科研成果体现形式字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/embodiment/list/count")
    public TableDataInfo researchOutcomesEmbodimentListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("research_outcomes_embodiment");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果阶段字典明细计数
     */
    @ApiOperation("查询科研成果阶段字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/stage/list/count")
    public TableDataInfo researchOutcomesStageListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("research_outcomes_stage");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询科研成果应用状态字典明细计数
     */
    @ApiOperation("查询科研成果应用状态字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/research/outcomes/application/state/list/count")
    public TableDataInfo researchOutcomesApplicationStateListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("research_outcomes_application_state");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询商业需求类型字典明细计数
     *
     * @return
     */
    @ApiOperation("查询商业需求类型字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/business/demand/type/list/count")
    public TableDataInfo businessDemandTypeListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("business_demand_type");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询商业需求合作方式字典明细计数
     *
     * @return
     */
    @ApiOperation("查询商业需求合作方式字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/business/demand/ways/cooperation/list/count")
    public TableDataInfo businessWaysCooperationListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("business_demand_ways_cooperation");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }

    /**
     * 查询商业需求合作方式字典明细计数
     *
     * @return
     */
    @ApiOperation("查询商业需求合作方式字典明细计数")
    @PreAuthorize("@ss.hasPermi('inn:system:list')")
    @GetMapping("/business/time/list/count")
    public TableDataInfo businessTimeListCount() {
        CodeSystem codeSystem = new CodeSystem();
        codeSystem.setCodeSystemNo("time_type");
        List<ResponseValueSetInfoCount> list = codeSystemService.selectCodeSystemListValueCount(codeSystem);
        return getDataTable(list);
    }
    //endregion 字典明细计数
}
